package cn.lingyangwl.agile.dynamic.client.wx;

import cn.lingyangwl.agile.dynamic.client.AbstractClientProcess;
import cn.lingyangwl.agile.dynamic.client.DynamicClient;
import cn.lingyangwl.agile.model.enums.ConfigDefineEnum;
import cn.lingyangwl.agile.model.module.config.param.WxMpParam;
import cn.lingyangwl.framework.core.utils.spring.SpringUtils;
import cn.lingyangwl.framework.tool.core.StringUtils;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.alibaba.fastjson2.JSON;
import com.binarywang.spring.starter.wxjava.mp.enums.HttpClientType;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author shenguangyang
 */
@Slf4j
@Data
@Component
@ConditionalOnClass(WxMpService.class)
@EqualsAndHashCode(callSuper = true)
public class WxMpDynamicConfig extends AbstractClientProcess {

    @Override
    public ConfigDefineEnum configDefine() {
        return ConfigDefineEnum.WX_MP;
    }

    @Override
    public DynamicClient createClient(String tenantId, String paramConfig) {
        log.info("refresh {} config", WxMpDynamicConfig.class.getName());
        if (StringUtils.isEmpty(paramConfig)) {
            throw new BizException("微信公众号配置参数不能为空");
        }
        WxMpWrapClient client = new WxMpWrapClient();
        // 这里读取自动配置的值
        WxMpProperties properties = SpringUtils.getBean(WxMpProperties.class);
        WxMpProperties.ConfigStorage configStorage = properties.getConfigStorage();

        // 初始哈存储配置
        WxMpParam wxMpParam = JSON.parseObject(paramConfig, WxMpParam.class);

        StringRedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        WxMpRedisConfigImpl wxMpConfigStorage = new WxMpRedisConfigImpl(new RedisTemplateWxRedisOps(redisTemplate), WxMpWrapClient.KEY_PRE);
        wxMpConfigStorage.setAppId(wxMpParam.getAppId());
        wxMpConfigStorage.setAesKey(wxMpParam.getEncodingAesKey());
        wxMpConfigStorage.setSecret(wxMpParam.getAppSecret());
        wxMpConfigStorage.setToken(wxMpParam.getToken());

        HttpClientType httpClientType = configStorage.getHttpClientType();
        WxMpService service;
        switch (httpClientType) {
            case OkHttp:
                service = new WxMpServiceOkHttpImpl();
                break;
            case JoddHttp:
                service = new WxMpServiceJoddHttpImpl();
                break;
            case HttpClient:
                service = new WxMpServiceHttpClientImpl();
                break;
            default:
                service = new WxMpServiceImpl();
        }
        service.setWxMpConfigStorage(wxMpConfigStorage);

        WxMpMessageRouter wxMpMessageRouter = new WxMpMessageRouter(service);
        client.setRouter(wxMpMessageRouter);
        client.setParams(wxMpParam);
        client.setService(service);
        return client;
    }
}
